package org.xtext.tcc;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.xbase.lib.ComparableExtensions;
import org.eclipse.xtext.xbase.lib.IntegerExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.eclipse.xtext.xtend2.lib.StringConcatenation;
import org.xtext.tcc.tccdsl.Attribute;
import org.xtext.tcc.tccdsl.Function;
import org.xtext.tcc.tccdsl.Package;
import org.xtext.tcc.tccdsl.Type;

@SuppressWarnings("all")
public class TccdslExtensions {
  
  public String packageName(final Object o) {
    String _switchResult = null;
    final Object o_1 = o;
    boolean matched = false;
    if (!matched) {
      if (o_1 instanceof Package) {
        final Package o_2 = (Package) o_1;
        matched=true;
        EObject _eContainer = o_2.eContainer();
        String _packageName = this.packageName(_eContainer);
        String _name = o_2.getName();
        String _concatPath = this.concatPath(_packageName, _name);
        _switchResult = _concatPath;
      }
    }
    if (!matched) {
      if (o_1 instanceof EObject) {
        final EObject o_3 = (EObject) o_1;
        matched=true;
        EObject _eContainer_1 = o_3.eContainer();
        String _packageName_1 = this.packageName(_eContainer_1);
        _switchResult = _packageName_1;
      }
    }
    if (!matched) {
      if (o_1 instanceof JvmDeclaredType) {
        final JvmDeclaredType o_4 = (JvmDeclaredType) o_1;
        matched=true;
        String _packageName_2 = o_4.getPackageName();
        _switchResult = _packageName_2;
      }
    }
    if (!matched) {
      _switchResult = null;
    }
    return _switchResult;
  }
  
  public String concatPath(final String prefix, final String suffix) {
    String _xifexpression = null;
    boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(prefix);
    if (_isNullOrEmpty) {
      _xifexpression = suffix;
    } else {
      String _operator_plus = StringExtensions.operator_plus(prefix, ".");
      String _operator_plus_1 = StringExtensions.operator_plus(_operator_plus, suffix);
      _xifexpression = _operator_plus_1;
    }
    return _xifexpression;
  }
  
  public StringConcatenation functionHeader(final Function f) {
    StringConcatenation _builder = new StringConcatenation();
    {
      Type _type = f.getType();
      boolean _operator_equals = ObjectExtensions.operator_equals(_type, null);
      if (_operator_equals) {
        _builder.append("void");} else {
        StringConcatenation _typeDeclaration = this.typeDeclaration(f);
        _builder.append(_typeDeclaration, "");
      }
    }
    _builder.append(" ");
    String _name = f.getName();
    _builder.append(_name, "");
    _builder.append("(");
    String _parameters = this.parameters(f);
    _builder.append(_parameters, "");
    _builder.append(")");
    return _builder;
  }
  
  public StringConcatenation typeDeclaration(final Attribute a) {
    StringConcatenation _builder = new StringConcatenation();
    {
      boolean _isMany = a.isMany();
      if (_isMany) {
        _builder.append("List<");
        Type _type = a.getType();
        String _name = _type.getName();
        _builder.append(_name, "");
        _builder.append(">");} else {
        Type _type_1 = a.getType();
        String _name_1 = _type_1.getName();
        _builder.append(_name_1, "");
      }
    }
    return _builder;
  }
  
  public StringConcatenation typeDeclaration(final Function f) {
    StringConcatenation _builder = new StringConcatenation();
    {
      boolean _isMany = f.isMany();
      if (_isMany) {
        _builder.append("List<");
        Type _type = f.getType();
        String _name = _type.getName();
        _builder.append(_name, "");
        _builder.append(">");} else {
        Type _type_1 = f.getType();
        String _name_1 = _type_1.getName();
        _builder.append(_name_1, "");
      }
    }
    return _builder;
  }
  
  public StringConcatenation variable(final Attribute a) {
    StringConcatenation _builder = new StringConcatenation();
    StringConcatenation _typeDeclaration = this.typeDeclaration(a);
    _builder.append(_typeDeclaration, "");
    _builder.append(" ");
    String _name = a.getName();
    _builder.append(_name, "");
    return _builder;
  }
  
  public String aspectParameters(final Function f) {
    String _xblockexpression = null;
    {
      String pars = "";
      int i = 0;
      EList<Attribute> _parameters = f.getParameters();
      for (Attribute p : _parameters) {
        {
          StringConcatenation _typeDeclaration = this.typeDeclaration(p);
          String _string = _typeDeclaration.toString();
          String _operator_plus = StringExtensions.operator_plus(pars, _string);
          pars = _operator_plus;
          int _operator_plus_1 = IntegerExtensions.operator_plus(((Integer)i), ((Integer)1));
          i = _operator_plus_1;
          EList<Attribute> _parameters_1 = f.getParameters();
          int _size = _parameters_1.size();
          boolean _operator_lessThan = ComparableExtensions.<Integer>operator_lessThan(((Integer)i), ((Integer)_size));
          if (_operator_lessThan) {
            String _operator_plus_2 = StringExtensions.operator_plus(pars, ", ");
            pars = _operator_plus_2;
          }
        }
      }
      _xblockexpression = (pars);
    }
    return _xblockexpression;
  }
  
  public String parameters(final Function f) {
    String _xblockexpression = null;
    {
      String pars = "";
      int i = 0;
      EList<Attribute> _parameters = f.getParameters();
      for (Attribute p : _parameters) {
        {
          StringConcatenation _variable = this.variable(p);
          String _string = _variable.toString();
          String _operator_plus = StringExtensions.operator_plus(pars, _string);
          pars = _operator_plus;
          int _operator_plus_1 = IntegerExtensions.operator_plus(((Integer)i), ((Integer)1));
          i = _operator_plus_1;
          EList<Attribute> _parameters_1 = f.getParameters();
          int _size = _parameters_1.size();
          boolean _operator_lessThan = ComparableExtensions.<Integer>operator_lessThan(((Integer)i), ((Integer)_size));
          if (_operator_lessThan) {
            String _operator_plus_2 = StringExtensions.operator_plus(pars, ", ");
            pars = _operator_plus_2;
          }
        }
      }
      _xblockexpression = (pars);
    }
    return _xblockexpression;
  }
  
  public String parameterDeclaration(final Function f) {
    String _xblockexpression = null;
    {
      String pardecl = "";
      int i = 0;
      EList<Attribute> _parameters = f.getParameters();
      for (Attribute p : _parameters) {
        {
          String _name = p.getName();
          String _operator_plus = StringExtensions.operator_plus(pardecl, _name);
          pardecl = _operator_plus;
          int _operator_plus_1 = IntegerExtensions.operator_plus(((Integer)i), ((Integer)1));
          i = _operator_plus_1;
          EList<Attribute> _parameters_1 = f.getParameters();
          int _size = _parameters_1.size();
          boolean _operator_lessThan = ComparableExtensions.<Integer>operator_lessThan(((Integer)i), ((Integer)_size));
          if (_operator_lessThan) {
            String _operator_plus_2 = StringExtensions.operator_plus(pardecl, ", ");
            pardecl = _operator_plus_2;
          }
        }
      }
      _xblockexpression = (pardecl);
    }
    return _xblockexpression;
  }
}